home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1539 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.3 KB  |  52 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news.csra.net!not-for-mail
  3. From: "Robert S. Stull" <stullr@dinamero.com>
  4. Subject: Re: Q: Returning a reference
  5. Message-ID: <30F53184.C57@dinamero.com>
  6. Date: Thu, 11 Jan 1996 10:49:24 -0500
  7. Organization: Ipswitch, Inc.
  8. X-Mailer: Mozilla 2.0b4 (WinNT; I)
  9. MIME-Version: 1.0
  10. CC: stullr-news@dinamero.com
  11. References: <4cvsm2$5ig@dub-news-svc-4.compuserve.com>
  12. Content-Type: text/plain; charset=us-ascii
  13. Content-Transfer-Encoding: 7bit
  14.  
  15. Martin Aupperle wrote:
  16. > Borland C++ V4.5 does not allow to return a reference to a local
  17. > variable:
  18. > int &doIt() {
  19. >   int i = 7;
  20. >   return i;  // syntax error
  21. >   }
  22. > I remember that I once had a compiler that did allow it (it gave me
  23. > only a warning).
  24. > Which one is right? I think that it should be an error because after
  25. > the function has terminated, the reference has no data object it is
  26. > bound to any more.
  27. > Martin
  28. > -----------------------------------
  29. > Signatures are a waste of bandwidth
  30. > -----------------------------------
  31.  
  32. Try this:
  33.     static int i = 7;
  34. Most compilers allow it and it will still be valid when you exit 
  35. the function.
  36.  
  37. Bob
  38. --
  39. Robert S. Stull                                                 
  40. Net Fool
  41. "Will work for food." - NOT!!!!!           "Will work for sex." 
  42. - NOT!!!
  43.    "Won't work for anything." -- THAT'S IT! YES! YES! THAT'S 
  44. MEEEE!!!
  45.  
  46.